﻿using System;
using System.Collections.Generic;
using System.Linq;
using nelech.Models.DBModel;
using System.Web;
using nelech.Controllers.Utils;
using System.Web.Mvc;
using System.IO;
using Google.GData.Extensions;
using Google.GData.Calendar;
using nelech.Models.CalendarModel;

namespace nelech.Controllers
{
    public partial class AdminController : Controller
    {
        public ActionResult LogOn()
        {
            return RedirectToAction("LogOn", "Account");
        }
        private void AddZimerImages(Zimer zimer)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (string.IsNullOrEmpty(file.FileName))
                    continue;

                var filename = SharedUtils.GetUniqueFileName(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                var image = new Image { path = SharedUtils.SaveImage(file.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                file.InputStream.Position = 0;
                SharedUtils.SaveImage(file.InputStream, "/Upload/Thumbs", filename, extension, Server, 50L);

                var fileKey = Request.Files.AllKeys[i];
                var labelName = "imageLabel" + fileKey.Substring("imagePath".Length);
                image.text = Request.Form[labelName];
                zimer.Image.Add(image);
            }
           
        }
        private void AddTrackImages(Track track)
        {
            HttpPostedFileBase map = Request.Files["map"];
            if (map != null && !string.IsNullOrEmpty(map.FileName))
            {
                var filename = SharedUtils.GetUniqueFileName(map.FileName);
                var extension = Path.GetExtension(map.FileName);
                var image = new Image { path = SharedUtils.SaveImage(map.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                track.map = image.path;
            }
            var startingIndex = map != null ? 1 : 0;
            for (int i = startingIndex; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (string.IsNullOrEmpty(file.FileName))
                    continue;

                var filename = SharedUtils.GetUniqueFileName(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                var image = new Image { path = SharedUtils.SaveImage(file.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                file.InputStream.Position = 0;
                SharedUtils.SaveImage(file.InputStream, "/Upload/Thumbs", filename, extension, Server, 50L);

                var fileKey = Request.Files.AllKeys[i];
                var labelName = "imageLabel" + fileKey.Substring("imagePath".Length);
                image.text = Request.Form[labelName];
                track.Image.Add(image);
            }
        }

        private void AddPlaceImages(Place p)
        {
            HttpPostedFileBase map = Request.Files["map"];
            if (map != null && !string.IsNullOrEmpty(map.FileName))
            {
                var filename = SharedUtils.GetUniqueFileName(map.FileName);
                var extension = Path.GetExtension(map.FileName);
                var image = new Image { path = SharedUtils.SaveImage(map.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                p.map = image.path;
            }

            var startingIndex = map != null ? 1 : 0;
            for (int i = startingIndex; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (string.IsNullOrEmpty(file.FileName))
                    continue;
                var filename = SharedUtils.GetUniqueFileName(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                SharedUtils.SaveImage(file.InputStream, "/Upload/Thumbs", filename, extension, Server, 50L);
                var image = new Image { path = SharedUtils.SaveImage(file.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                var fileKey = Request.Files.AllKeys[i];
                var labelName = "imageLabel" + fileKey.Substring("imagePath".Length);
                image.text = Request.Form[labelName];
                p.Image.Add(image);
            }
        }
        private void AddMunachImages(Munach p)
        {

            for (int i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (string.IsNullOrEmpty(file.FileName))
                    continue;
                var filename = SharedUtils.GetUniqueFileName(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                SharedUtils.SaveImage(file.InputStream, "/Upload/Thumbs", filename, extension, Server, 50L);
                var image = new Image { path = SharedUtils.SaveImage(file.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                var fileKey = Request.Files.AllKeys[i];
                var labelName = "imageLabel" + fileKey.Substring("imagePath".Length);
                image.text = Request.Form[labelName];
                p.Image.Add(image);
            }
        }

        private void AddSpringImages(Maayan m)
        {
            HttpPostedFileBase map = Request.Files["map"];
            if (map != null && !string.IsNullOrEmpty(map.FileName))
            {
                var filename = SharedUtils.GetUniqueFileName(map.FileName);
                var extension = Path.GetExtension(map.FileName);
                var image = new Image { path = SharedUtils.SaveImage(map.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                m.map = image.path;
            }
            var startingIndex = map != null ? 1 : 0;
            for (int i = startingIndex; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (string.IsNullOrEmpty(file.FileName))
                    continue;
                var filename = SharedUtils.GetUniqueFileName(file.FileName);
                var extension = Path.GetExtension(file.FileName);

                var image = new Image { path = SharedUtils.SaveImage(file.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                SharedUtils.SaveImage(file.InputStream, "/Upload/Thumbs", filename, extension, Server, 50L);
                var fileKey = Request.Files.AllKeys[i];
                var labelName = "imageLabel" + fileKey.Substring("imagePath".Length);
                image.text = Request.Form[labelName];
                m.Image.Add(image);
            }
        }

        private void BindTrack(Track track, int _cost, int _difficulty, int _duration, int _regeon, int _time, int _circle)
        {
            track.Cost1 = db.Cost.First(p => p.id == _cost);
            track.Difficulty1 = db.Difficulty.First(p => p.id == _difficulty);
            track.Duration1 = db.Duration.First(p => p.id == _duration);
            track.Time1 = db.Time.First(p => p.id == _time);
            track.Regeon1 = db.Regeon.First(p => p.id == _regeon);
            track.Circle1 = db.Circle.First(p => p.id == _circle);

        }

        private void BindPlace(Place place, int _cost, int _regeon)
        {
            place.Cost1 = db.Cost.First(p => p.id == _cost);
            place.Regeon1 = db.Regeon.First(p => p.id == _regeon);

        }

        private void BindSpring(Maayan m, int _cost, int _nevia, int _regeon)
        {
            m.Cost1 = db.Cost.First(p => p.id == _cost);
            m.Nevia1 = db.Nevia.First(p => p.id == _nevia);
            m.Regeon1 = db.Regeon.First(p => p.id == _regeon);
        }

        private void BindZimer(Zimer m, int _regeon)
        {
            m.Regeon1 = db.Regeon.First(p => p.id == _regeon);
        }

        private void BindTrackToMultiselectionEntities(Track track)
        {
            foreach (var traffic in db.Traffic)
            {
                var cb = Request.Form["traffic" + traffic.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    track.Traffic.Add(traffic);
                }
            }
            foreach (var subject in db.Subject)
            {
                var cb = Request.Form["subject" + subject.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    track.Subject.Add(subject);
                }
            }
            foreach (var season in db.Season)
            {
                var cb = Request.Form["season" + season.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    track.Season.Add(season);
                }
            }
        }
        private void BindSpringToMultiselectionEntities(Maayan mayyan)
        {
           
            foreach (var subject in db.Subject)
            {
                var cb = Request.Form["subject" + subject.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    mayyan.Subject.Add(subject);
                }
            }
            foreach (var season in db.Season)
            {
                var cb = Request.Form["season" + season.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    mayyan.Season.Add(season);
                }
            }
        }
        private void BindPlaceToMultiselectionEntities(Place place)
        {
            foreach (var type in db.Type)
            {
                var cb = Request.Form["type" + type.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    place.Type.Add(type);
                }
            }
            foreach (var subject in db.Subject)
            {
                var cb = Request.Form["subject" + subject.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    place.Subject.Add(subject);
                }
            }
            foreach (var season in db.Season)
            {
                var cb = Request.Form["season" + season.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    place.Season.Add(season);
                }
            }
        }
       
        private void BindZimerToMultiselectionEntities(Zimer zimer)
        {
            foreach (var size in db.Size)
            {
                var cb = Request.Form["size" + size.id];
                var isChecked = cb.StartsWith("true");
                if (isChecked)
                {
                    zimer.Size.Add(size);
                }
            }
        }
        private void UnbindTrackFromMultiselectionEntities(Track p)
        {
            p.Traffic.Load();
            foreach (var traffic in p.Traffic.ToArray())
            {
                p.Traffic.Remove(traffic);
            }

            p.Subject.Load();
            foreach (var subject in p.Subject.ToArray())
            {
                p.Subject.Remove(subject);
            }

            p.Season.Load();
            foreach (var season in p.Season.ToArray())
            {
                p.Season.Remove(season);
            }
        }
        private void UnbindPlaceFromMultiselectionEntities(Place p)
        {
            p.Type.Load();
            foreach (var type in p.Type.ToArray())
            {
                p.Type.Remove(type);
            }
            p.Subject.Load();
            foreach (var subject in p.Subject.ToArray())
            {
                p.Subject.Remove(subject);
            }

            p.Season.Load();
            foreach (var season in p.Season.ToArray())
            {
                p.Season.Remove(season);
            }
        }

        private void UnbindSpringFromMultiselectionEntities(Maayan p)
        {
           
            p.Subject.Load();
            foreach (var subject in p.Subject.ToArray())
            {
                p.Subject.Remove(subject);
            }

            p.Season.Load();
            foreach (var season in p.Season.ToArray())
            {
                p.Season.Remove(season);
            }
        }

        private void UnbindZimerFromMultiselectionEntities(Zimer z)
        {
            z.Size.Load();
            foreach (var size in z.Size.ToArray())
            {
                z.Size.Remove(size);
            }
        }

        private void UpdateEventEntryProperties(EventEntry entry, Event e)
        {
            entry.Title.Text = e.Title;
            entry.Content.Content = e.Details;
            var startTime = new DateTime(e.Year, e.Month, e.Day, e.StartHour, e.StartMinute, 0);
            var endTime = new DateTime(e.Year, e.Month, e.Day, e.EndHour, e.EndMinute, 0);
            entry.Times.Add(new When(startTime, endTime));

            var wcl = new WebContentLink(true);
            wcl.Title = e.Title;
            //var host = "http://localhost:51232";
            var host = "http://nelech.co.il";
            if (!string.IsNullOrEmpty(e.MceDoc))
            {
                var docUrl = host + SharedUtils.SaveDoc(e.MceDoc, "/Upload/Texts", Server);
                wcl.WebContent.Url = docUrl;
            }
            wcl.WebContent.Width = 400;
            wcl.WebContent.Height = 400;

            HttpPostedFileBase map = Request.Files["Photo"];
            if (map != null && !string.IsNullOrEmpty(map.FileName))
            {
                var filename = SharedUtils.GetUniqueFileName(map.FileName);
                var extension = Path.GetExtension(map.FileName);
                var imagePath = SharedUtils.SaveImage(map.InputStream, "/Upload/Images", filename, extension, Server, 100L);
                var url = host + imagePath;
                wcl.HRef = url;
            }
            else
            {
                wcl.HRef = host + "/Upload/Images/eventDefault.png";
            }

            entry.WebContentLink = wcl;
        }
        private void AddPuzzleImages(PuzzleTopic puzzleTopic)
        {
            HttpPostedFileBase image = Request.Files["image"];
            if (image != null && !string.IsNullOrEmpty(image.FileName))
            {
                var filename = SharedUtils.GetUniqueFileName(image.FileName);
                var extension = Path.GetExtension(image.FileName);
                var image1 = new Image { path = SharedUtils.SaveImage(image.InputStream, "/Upload/Images", filename, extension, Server, 100L) };
                puzzleTopic.image = image1.path;
            }
        }




    }
}